﻿using System.Xml.Linq;
using BattleFury.Lib.Extensions;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Controls
{
	public class Border
		: Control
	{
		public static string PartTopLeft = "WindowTopLeft";
		public static string PartTopCenter = "WindowTopCenter";
		public static string PartTopRight = "WindowTopRight";
		public static string PartLeft = "WindowLeft";
		public static string PartRight = "WindowRight";
		public static string PartBottomLeft = "WindowBottomLeft";
		public static string PartBottomCenter = "WindowBottomCenter";
		public static string PartBottomRight = "WindowBottomRight";

		public Color BorderColor { get; set; }

		public override void Deserialize(XElement element, object callingObject)
		{
			base.Deserialize(element, callingObject);

			BorderColor = element.GetColor("BorderColor", Color.White);
		}

		public override void Draw(GameTime gameTime)
		{
			var topLeftPart = Skin[PartTopLeft];
			var topCenterPart = Skin[PartTopCenter];
			var topRightPart = Skin[PartTopRight];
			var leftPart = Skin[PartLeft];
			var rightPart = Skin[PartRight];
			var bottomLeftPart = Skin[PartBottomLeft];
			var bottomCenterPart = Skin[PartBottomCenter];
			var bottomRightPart = Skin[PartBottomRight];

			var innerWidth = Size.X - topLeftPart.Width - topRightPart.Width;
			var innerHeight = Size.Y - topLeftPart.Height - bottomLeftPart.Height;
			var xOffset = 0f;
			var yOffset = 0f;

			// Top Left
			SpriteBatch.Draw(Texture, Position, topLeftPart.SourceBounds, BorderColor);

			// Top Center
			for (var i = 0; i < innerWidth; i += topCenterPart.Width)
			{
				SpriteBatch.Draw(Texture, Position + new Vector2(topLeftPart.Width + i, 0f), topCenterPart.SourceBounds, BorderColor);
				xOffset = (topLeftPart.Width + i);
			}

			// Top Right
			SpriteBatch.Draw(Texture, Position + new Vector2(xOffset + topCenterPart.Width, 0f), topRightPart.SourceBounds, BorderColor);

			// Left
			for (var i = 0; i < innerHeight; i += leftPart.Height)
			{
				SpriteBatch.Draw(Texture, Position + new Vector2(0f, topLeftPart.Height + i), leftPart.SourceBounds, BorderColor);
				yOffset = (topLeftPart.Height + i);
			}

			// Right
			for (var i = 0; i < innerHeight; i += leftPart.Height)
			{
				SpriteBatch.Draw(Texture, Position + new Vector2(xOffset + topCenterPart.Width, topRightPart.Height + i), rightPart.SourceBounds, BorderColor);
			}

			// Bottom Left
			SpriteBatch.Draw(Texture, Position + new Vector2(0f, yOffset + leftPart.Height), bottomLeftPart.SourceBounds, BorderColor);

			// Bottom Center
			for (var i = 0; i < innerWidth; i += topCenterPart.Width)
			{
				SpriteBatch.Draw(Texture, Position + new Vector2(bottomLeftPart.Width + i, yOffset + rightPart.Height), bottomCenterPart.SourceBounds, BorderColor);
			}

			// Bottom Right
			SpriteBatch.Draw(Texture, Position + new Vector2(xOffset + topCenterPart.Width, yOffset + rightPart.Height), bottomRightPart.SourceBounds, BorderColor);

		}
	}
}